//
//  MainMenuViewController.m
//  CloudKaraoke
//
//  Created by BHDMinh on 3/24/14.
//  Copyright (c) 2014 soncamedia. All rights reserved.
//

#import "MainMenuViewController.h"
//#import "KaraokeViewController.h"
//#import "KaraokeControlVC.h"
//#import "Playlists.h"

@interface MainMenuViewController ()

@end

@implementation MainMenuViewController
@synthesize searchBarButtonItem;
@synthesize karaokeButtonItem;

#pragma mark - UIBarButtonItems
- (void)setupMenuBarButtonItems {
    if(/*self.menuContainerViewController.menuState == MFSideMenuStateClosed &&*/
       ![[self.navigationController.viewControllers objectAtIndex:0] isEqual:self]) {
        self.navigationItem.leftBarButtonItem = [self backBarButtonItem];
    } else {
        self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
    }
//    NSArray *rightItems = [NSArray arrayWithObjects:
//                           [self rightMenuBarButtonItem],
//                           [self searchBarButtonItem],
//                           nil];
    
    NSMutableArray *rightItems = [NSMutableArray array];
    if (!_karaokeHidden) {
        [rightItems addObject:[self karaokeButtonItem]];
    }
    if (!_searchHidden) {
        [rightItems addObject:[self searchBarButtonItem]];
    }
    
//    self.navigationItem.rightBarButtonItem = [self rightMenuBarButtonItem];
    [self.navigationItem setRightBarButtonItems:rightItems];
}

- (UIBarButtonItem *)leftMenuBarButtonItem {
    if (IS_IPAD) {
        return nil; 
    }
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 57.0, 44.0)];
    [button addTarget:self action:@selector(leftSideMenuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"ico-menu.png"] forState:UIControlStateNormal];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (UIBarButtonItem *)karaokeButtonItem {
    if(karaokeButtonItem)
        return karaokeButtonItem;
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44.0, 44.0)];
    [button addTarget:self action:@selector(rightSideMenuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"ico-karaoke.png"] forState:UIControlStateNormal];
    
    karaokeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//    if ([Playlists getSongCount] > 0) {
//        [karaokeButtonItem setEnabled:YES];
//    }else {
//        [karaokeButtonItem setEnabled:NO];
//    }
    return karaokeButtonItem;
}

-(void) updateKaraokeBarButton
{
    if (!karaokeButtonItem) {
        return;
    }
    
//    if ([Playlists getSongCount] > 0) {
//        [karaokeButtonItem setEnabled:YES];
//    }else {
//        [karaokeButtonItem setEnabled:NO];
//    }
}

- (UIBarButtonItem *)backBarButtonItem {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44.0, 44.0)];
    [button addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
//    UIImage *bgImage = [UIImage imageNamed:@"ico-back.png"];
    
    [button setImage:[UIImage imageNamed:@"ico-back.png"] forState:UIControlStateNormal];
//    int numViews = [self.navigationController.viewControllers count];
//    if(numViews > 1)
//    {
//        UIViewController *parentVC = [self.navigationController.viewControllers objectAtIndex:numViews - 1 - 1];
//        [button setTitle:parentVC.title forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [[button titleLabel] setFont:[UIFont fontWithName:@"Helvetica" size:15.0]];
//        [[button titleLabel] setTextColor:KOK_COLOR_AUTHOR];
//        [[button titleLabel] setTextAlignment:NSTextAlignmentLeft];
//    }
    
    CGRect frame = [button.imageView frame];
    frame.origin = CGPointMake(0, 0);
    [button.imageView setFrame:frame]; 
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void) setKaraokeButtonItemHidden:(BOOL) hidden
{
    _karaokeHidden = hidden;
    if(karaokeButtonItem == nil || [karaokeButtonItem customView] == nil) return;
    
    [[karaokeButtonItem customView] setHidden:hidden];
}

- (void) setSearchBarButtonItemHidden:(BOOL)hidden
{
    _searchHidden = hidden;
    if(searchBarButtonItem == nil || [searchBarButtonItem customView] == nil) return;
    
    [[searchBarButtonItem customView] setHidden:hidden];
}

- (UIBarButtonItem *) searchBarButtonItem
{
    if(searchBarButtonItem != nil)
        return searchBarButtonItem;
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"ico-search.png"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchButtonDidPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    return searchBarButtonItem;
}

#pragma mark - UIBarButtonItem Callbacks
- (void) backToPreviousView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)backButtonPressed:(id)sender {
    [self backToPreviousView];
}

- (void)leftSideMenuButtonPressed:(id)sender {
//    [self.menuContainerViewController toggleLeftSideMenuCompletion:^{
        [self setupMenuBarButtonItems];
//        if (self.menuContainerViewController.menuState == MFSideMenuStateClosed) {
//            
//        }
//    }];
}

- (void)rightSideMenuButtonPressed:(id)sender {
//    [self.menuContainerViewController toggleRightSideMenuCompletion:^{
//        [self setupMenuBarButtonItems];
//    }];
//    
////    KaraokeControlVC *kokController = [[KaraokeControlVC alloc] initWithNibName:@"KaraokeControlVC" bundle:nil];
////    [self.navigationController pushViewController:kokController animated:YES];
//    
//    if([Playlists getSongCount] > 0)
//    {
//        KaraokeViewController *kokPlayer = nil;
//        if(IS_IPAD) {
//            kokPlayer = [[KaraokeViewController alloc] initWithNibName:@"KaraokeViewController-ipad" bundle:nil];
//        }else {
//            kokPlayer = [[KaraokeViewController alloc] initWithNibName:@"KaraokeViewController" bundle:nil];
//        }
//        [self.navigationController pushViewController:kokPlayer animated:YES];
//    }
}

- (void) reloadDisplayData
{
    
}

- (void) searchBarButtonItemDidPresses
{
    
}

- (void) searchButtonDidPressed:(id) sender
{
    [self searchBarButtonItemDidPresses];
}

- (id) init
{
    if(!(self = [super init]))
    {
        _searchHidden = FALSE;
        _karaokeHidden = FALSE;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _searchHidden = FALSE;
        _karaokeHidden = FALSE;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupMenuBarButtonItems];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL) prefersStatusBarHidden
{
    return NO;
}

- (void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews]; 
}

- (void) viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
//    [self.menuContainerViewController setRightMenuWidth:self.view.frame.size.width]; 
}

#pragma mark - UIRotationHandler
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

- (BOOL) shouldAutorotate
{
    return YES;
}

- (NSUInteger) supportedInterfaceOrientations
{
    if(!IS_IPAD)
        return UIInterfaceOrientationMaskAllButUpsideDown; 
    return UIInterfaceOrientationMaskAll;
}
@end
